<html>
<head><meta charset="utf-8"><title>Lifetime elision bug? trait object &amp; function case · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20elision.20bug.3F.20trait.20object.20.26.20function.20case.html">Lifetime elision bug? trait object &amp; function case</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="217626629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20elision%20bug%3F%20trait%20object%20%26%20function%20case/near/217626629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20elision.20bug.3F.20trait.20object.20.26.20function.20case.html#217626629">(Nov 23 2020 at 13:22)</a>:</h4>
<p>Quoting <a href="https://doc.rust-lang.org/reference/lifetime-elision.html#default-trait-object-lifetimes">the reference</a>, regarding trait object lifetimes:</p>
<blockquote>
<p>These default object lifetime bounds are used instead of the lifetime parameter elision rules defined above when the lifetime bound is omitted entirely. <strong>If <code>'_</code> is used as the lifetime bound then the bound follows the usual elision rules.</strong></p>
</blockquote>
<p>This does not match some cases of (failed) lifetime elision, mainly:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">bar</span><span class="w"> </span><span class="p">(</span><span class="n">_</span>: <span class="nb">Box</span><span class="o">&lt;</span><span class="k">dyn</span><span class="w"> </span><span class="nb">Sync</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="o">'</span><span class="nb">_</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="o">'</span><span class="nb">_</span> <span class="p">()</span><span class="w"> </span><span class="c1">// Fails!??</span>
</code></pre></div>
<ul>
<li><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=aa23ba284dfd7f165ef875e5e1beb46a">Demo</a></li>
</ul>
<hr>
<p>Is this a bug in the compiler, or in the reference?</p>
<hr>
<p>(For context, <a href="https://github.com/danielhenrymantilla/with_locals.rs/pull/9">I have a proc-macro which needs to apply the lifetime (un)elision rules on its own</a>, so I am trying really hard not to deviate from the standard behavior.</p>



<a name="217628436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20elision%20bug%3F%20trait%20object%20%26%20function%20case/near/217628436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20elision.20bug.3F.20trait.20object.20.26.20function.20case.html#217628436">(Nov 23 2020 at 13:40)</a>:</h4>
<p>Could it be a semantics thing? "as the lifetime bound" to me means:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="n">T</span>: <span class="o">'</span><span class="na">some_lifetime</span>
</code></pre></div>
<p>Not as part of the type, as shown in your snippet. I don't know if that's canonical or not.</p>



<a name="217629695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20elision%20bug%3F%20trait%20object%20%26%20function%20case/near/217629695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20elision.20bug.3F.20trait.20object.20.26.20function.20case.html#217629695">(Nov 23 2020 at 13:52)</a>:</h4>
<p>It seems to me that when they talk of lifetime bounds with a trait object, unless generic parameters are mentioned, it seems to refer to the <code>+ 'here</code> part of a trait object. Since a trait object name / signature can be seen as "just" a bunch of bounds (that the before-erasure types had to meet).</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>